﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Net.Mail;
using System.Net;
using System.IO;

namespace Reconciliation.Web
{
    public partial class Mail : System.Web.UI.Page
    {
        public ArrayList AttachFiles = new ArrayList();
                                
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Session["AttachFiles"] = null;
                AddFileUpdalod();
            }

            if (Session["AttachFiles"] != null)
            {
                AttachFiles = (ArrayList)Session["AttachFiles"];
            }
        }

        private void AddFileUpdalod()
        {
            //int FileCount = 1;
            //if (datalits.Items.Count > 0)
            //    FileCount = datalits.Items.Count + 1;

            ArrayList arrFile=new ArrayList();
            for (int i = 0; i < AttachFiles.Count; i++)
            {
                arrFile.Add("File " + (i + 1).ToString() + ": " + AttachFiles[i].ToString());
            }

            datalits.DataSource = arrFile;
            datalits.DataBind();
        }

        private void SendMail(string MailTo, string MailToCC, string Subject, string Message)
        {
            SmtpClient smtpClient = new SmtpClient("iraqcarsale.com");
            smtpClient.Credentials = new NetworkCredential("admin@iraqcarsale.com", "iraq2009");

            MailMessage message = new MailMessage();

            try
            {

                MailAddress fromAddress = new MailAddress("admin@iraqcarsale.com", "Admin");

                message.From = fromAddress;//here you can set address

                message.To.Add(MailTo);//here you can add multiple to

                message.Subject = Subject;//subject of email

                if (MailToCC != "")
                    message.CC.Add(MailToCC);//ccing the same email to other email address

                //message.Bcc.Add(new MailAddress("bcc@site.com"));//here you can add bcc address
                for (int k = 0; k < AttachFiles.Count; k++)
                {
                    string filePath = Server.MapPath("Attachments") + "\\" + AttachFiles[k].ToString();
                    message.Attachments.Add(new System.Net.Mail.Attachment(filePath));
                }
                
                message.IsBodyHtml = true;//To determine email body is html or not

                message.Body = Message;

                smtpClient.Send(message);

                // Delete Stored Files
                for (int k = 0; k < AttachFiles.Count; k++)
                {
                    message.Attachments[k].Dispose();

                    string filePath = Server.MapPath("Attachments") + "\\" + AttachFiles[k].ToString();
                    if (File.Exists(filePath))
                    {
                        try
                        {
                            File.Delete(filePath);
                        }
                        catch (Exception ex)
                        {
                            // trow exception.
                        }
                    }
                    Session["AttachFiles"] = null;
                }

                lblMsg.Text = "Mail successfully sent.";
                lblMsg.ForeColor = System.Drawing.Color.Green;
            }

            catch (Exception ex)
            {

                //throw exception here you can write code to handle exception here
                lblMsg.Text = ex.Message;
                lblMsg.ForeColor = System.Drawing.Color.Red;

            }
        }
        protected void btnAddMoreFile_Click(object sender, EventArgs e)
        {
            AddFileUpdalod();
        }

        protected void btnAttach_Click(object sender, EventArgs e)
        {
            UploadFiles();
        }

        private void UploadFiles()
        {
            //foreach (DataListItem item in datalits.Items)
            //{
            //    FileUpload file = new FileUpload();

            //    file = (FileUpload)item.FindControl("AttachFile");

                
            //}

            if (AttachFile.PostedFile != null)
            {
                HttpPostedFile ulFile = AttachFile.PostedFile;
                if (ulFile.ContentLength > 0)
                {
                    string strFileName = System.IO.Path.GetFileName(AttachFile.PostedFile.FileName);
                    // Preced the file name with "attachments/" so 

                    // the file is saved to our attachments directory

                    string strFilePath = "Attachments/" + strFileName;
                    // Save the file on the server

                    AttachFile.PostedFile.SaveAs(Server.MapPath(strFilePath));
                    // Create the email attachment with the uploaded file

                    AttachFiles.Add(strFileName);
                }
            }
            
            Session["AttachFiles"] = AttachFiles;
            AddFileUpdalod();
        }
                
        protected void btnSendMail_Click(object sender, EventArgs e)
        {
            SendMail(txtMailTo.Text, txtMailCc.Text, txtSubject.Text, txtMessage.Text);
        }
}
}
